local EmoTalentUtil = require "packages.emopack.pkg.emopack_talent.util" -- 加载战法核心
local skel = require "packages.emopack.pkg.emopack_talent.talents.core" ---@type SkillSkeleton

local hasTalent = EmoTalentUtil.hasTalent
local getTalents = EmoTalentUtil.getTalents

-- 状态技战法

--- 牌合法性技
skel:addEffect("targetmod", {
  bypass_times = function (self, player, skill, scope, card, to)
    if hasTalent(player, "emot_jiugui") and card.trueName == "analeptic" then -- 酒鬼
      return true
    end
    if card.trueName == "slash" then
      if hasTalent(player, "emot_zhanshi") then return true end -- 战士
      if hasTalent(player, "emot_huxiao") then return true end -- 虎啸
    end
  end,
  bypass_distances = function (self, player, skill, card, to)
    if hasTalent(player, "emot_yingji") then -- 鹰击
      return true
    end
  end,
  residue_func = function (self, player, skill, scope, card, to)
    local n = 0
    if skill and skill.trueName == "slash_skill" then -- 【杀】次数上限
      if hasTalent(player, "emot_beastking") then -- 兽王
        n = n + #getTalents(player, "beast")
      end
      if hasTalent(player, "emot_jiedian") then -- 节电模式
        n = n + 2
      end
      if hasTalent(player, "emot_tieyi") then -- 铁翼天使
        n = n + 5
      end
    end
    return n
  end,
})

--- 攻击范围加成
skel:addEffect("atkrange", {
  correct_func = function (self, player, to)
    local room = Fk:currentRoom()
    local n = 0
    if hasTalent(player, "emot_zhanshi") then -- 战士
      n = 3
    end
    if hasTalent(player, "emot_beastking") then -- 兽王
      n = n + #getTalents(player, "beast")
    end
    n = n - #table.filter(room.alive_players, function (p)
      return hasTalent(p, "emot_zhimang") and p:isEnemy(player) -- 致盲术
    end)
    return n
  end,
})

--- 手牌上限加成
skel:addEffect("maxcards", {
  exclude_from = function (self, player, card)
    if hasTalent(player, "emot_yiqun") and card.trueName == "slash" then -- 蚁群
      return true
    end
    if hasTalent(player, "emot_jianiang") and card.trueName == "analeptic" then -- 佳酿
      return true
    end
    if hasTalent(player, "emot_zoudi") and card.color == Card.Red then -- 走地菇
      return true
    end
  end,
  fixed_func = function (self, player)
    if hasTalent(player, "emot_kuangshu") then -- 狂鼠
      return 0
    end
  end,
  correct_func = function (self, player) -- 加减数值
    local n = 0
    if hasTalent(player, "emot_beastking") then -- 兽王
      n = n + #getTalents(player, "beast")
    end
    if hasTalent(player, "emot_woniu") then -- 旅行蜗牛
      n = n + 5
    end
    return n
  end,
})

--- 距离技
skel:addEffect("distance", {
  correct_func = function (self, from, to)
    local room = Fk:currentRoom()
    local n = - from:getMark("emot_baoma-phase") -- 宝马
    if to:isEnemy(from) then
      n = n + 3 * #table.filter(room.alive_players, function (p)
        return p:isFriend(to) and hasTalent(p, "emot_judi") -- 拒敌千里
      end)
    end
    return n
  end,
})

--- 可见性技
skel:addEffect("visibility", {
  card_visible = function (self, player, card)
    local room = Fk:currentRoom()
    if room:getCardArea(card) == Card.PlayerHand then
      if table.find(room.alive_players, function (p) -- 晴天
        return hasTalent(p, "emot_qingtian")
      end) then
        return true
      end
    end
  end,
})

--- 技能失效技
skel:addEffect("invalidity", {
  invalidity_func = function(self, player, skill)
    if skill:isPlayerSkill(player) then
      local room = Fk:currentRoom()
      local current = room:getCurrent()
      local skelName = skill:getSkeleton().name
      if player:getMark("emot_invalid-turn") ~= 0 or
       table.contains(player:getTableMark("emot_invalid_skills-turn"), skill:getSkeleton().name) then -- 通用
        return true
      end
      if not table.contains({"wei", "shu", "wu"}, player.kingdom) then -- 三分天下
        if current and table.find(room.alive_players, function (p)
          return p:isFriend(current) and p:isEnemy(player) and hasTalent(p, "emot_sanfen")
        end) then
          return true
        end
      else
        if current and table.find(room.alive_players, function (p) -- 三国归晋
          return p:isFriend(current) and p:isEnemy(player) and hasTalent(p, "emot_guijin")
        end) then
          return true
        end
      end
      if player.chained and current and current:isEnemy(player) and hasTalent(current, "emot_suojiang") then -- 锁匠
        return true
      end
      -- 弱点反制模组
      if table.find(room.alive_players, function (p) return p:isEnemy(player) and hasTalent(p, "emot_ruodian") end) then
        if not (Fk.generals[player.general] and table.contains(Fk.generals[player.general]:getSkillNameList(true), skelName))
        and not (Fk.generals[player.deputyGeneral] and table.contains(Fk.generals[player.deputyGeneral]:getSkillNameList(false), skelName)) then
          return true
        end
      end
    end
  end
})

--- 禁止技
skel:addEffect("prohibit", {
  prohibit_response = function (self, player, card)
    if player:getMark("emot_prohibit_response-turn") ~= 0 then return true end
  end,
  is_prohibited = function (self, from, to, card)
    if not card or not to then return false end
    local room = Fk:currentRoom()
    if card.trueName == "savage_assault" then -- 蛮王
      if table.find(room.alive_players, function (p)
        return hasTalent(p, "emot_manwang") and p:isFriend(to)
      end) then
        return true
      end
    elseif card.trueName == "slash" then
      if hasTalent(to, "emot_yinshen") and card.color == Card.Black then -- 隐身术
        return true
      end
    end
    if from and from:getMark("emot_xinxisu-turn") ~= 0 and from ~= to then -- 信息素陷阱
      return true
    end
  end,
  prohibit_use = function (self, player, card)
    local cards = Card:getIdList(card)
    local dbmark = player:getTableMark("emot_diebao-turn") -- 谍报
    if #dbmark > 0 and table.find(cards, function (id) return table.contains(dbmark, id) end) then
      return true
    end
    if player.chained and table.find(Fk:currentRoom().alive_players, function (p) -- 结网
      return p:isEnemy(player) and hasTalent(p, "emot_jiewang")
    end) then
      return true
    end
    -- 思想禁锢
    if card.trueName == "nullification" or card.trueName == "analeptic"
    or card.sub_type == Card.SubtypeDefensiveRide or card.sub_type == Card.SubtypeOffensiveRide then
      if table.find(Fk:currentRoom().alive_players, function (p) return p:isEnemy(player) and hasTalent(p, "emot_jingu") end) then
        return true
      end
    end
  end,
})

--- 锁定视为技
skel:addEffect("filter", {
  mute = true,
  card_filter = function(self, card, player)
    return type(card:getMark("@@emot_qimeila-inhand")) == "string"
  end,
  view_as = function(self, player, card)
    local c = Fk:cloneCard(card:getMark("@@emot_qimeila-inhand"), card.suit, card.number)
    -- c.skillName = skel.name
    return c
  end,
})
